package fylder.recycler.demo.tools;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

/**
 * Created by fylder on 15-6-30.
 */
public class AnimatorUtils {

    /**
     * 显示
     *
     * @param view
     */
    public static void show(final View view) {
        show(view, 700);
    }

    /**
     * @param view
     * @param duration
     */
    public static void show(final View view, long duration) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 0.2f, 1f, 1));
            set.setDuration(duration);
            set.start();
        }
    }

    /**
     * 隐藏
     *
     * @param view
     */
    public static void hide(final View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 0));
            set.setDuration(700);
            set.start();
        }
    }

    public static void scaleHide(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 0),
                    ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.1f),
                    ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.1f));
            set.setDuration(700);
            set.start();
        }
    }

    public static void scaleShow(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 0f, 1f, 1),
                    ObjectAnimator.ofFloat(view, "scaleX", 0.1f, 1f),
                    ObjectAnimator.ofFloat(view, "scaleY", 0.1f, 1f));
            set.setDuration(700);
            set.start();
        }
    }

    /**
     * 上移动
     *
     * @param view  logo
     * @param view2 input
     */

    public static void moveTop(View view, View view2) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();

            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 0.5f),
                    ObjectAnimator.ofFloat(view, "scaleX", 1, 0.6f),
                    ObjectAnimator.ofFloat(view, "scaleY", 1, 0.6f),
                    ObjectAnimator.ofFloat(view, "translationY", 0, -view.getHeight() / 2));
            set.setDuration(700);
            set.setInterpolator(new DecelerateInterpolator());
            set.start();


            AnimatorSet set2 = new AnimatorSet();
            set2.playTogether(ObjectAnimator.ofFloat(view2, "translationY", 0, -view.getHeight()));
            set2.setDuration(700);
            set2.setInterpolator(new DecelerateInterpolator());
            set2.setStartDelay(100);
            set2.start();
        }
    }

    /**
     * 向下移动
     *
     * @param view
     */
    public static void moveBack(View view, View view2) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(view, "alpha", 0.5f, 1f, 1),
                    ObjectAnimator.ofFloat(view, "scaleX", 0.6f, 1f),
                    ObjectAnimator.ofFloat(view, "scaleY", 0.6f, 1f),
                    ObjectAnimator.ofFloat(view, "translationY", -view.getHeight() / 2, 0));
            set.setDuration(700);//设置动画时间
            set.setInterpolator(new DecelerateInterpolator());
            set.setStartDelay(200);
            set.start();

            AnimatorSet set2 = new AnimatorSet();
            set2.playTogether(ObjectAnimator.ofFloat(view2, "translationY", -view.getHeight(), 0));
            set2.setDuration(700);
            set2.setInterpolator(new DecelerateInterpolator());
            set2.start();
        }
    }

//    /**
//     * 上移渐显
//     *
//     * @param view
//     * @param y
//     */
//    public static void showTop(View view, int y) {
//        showTop(view, 0, y);
//    }


//    public static void showTop(final View view, int d, int y) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            ObjectAnimator animator = ObjectAnimator.ofFloat(view, "Y", view.getY() + DisplayUtils.dp2px(AppData.getContext(), y), view.getY());
//            animator.setDuration(400);
//            animator.setInterpolator(getMaterialInterpolator(AppData.getContext()));
//            animator.setStartDelay(d);
//            animator.start();
//
////        animator.addListener(new AnimatorListenerAdapter() {
////
////            @Override
////            public void onAnimationStart(Animator animation) {
////                super.onAnimationStart(animation);
////                view.setVisibility(View.VISIBLE);//显示
////            }
////        });
//
//            ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
//            animator2.setDuration(400);
//            animator2.setInterpolator(getMaterialInterpolator(AppData.getContext()));
//            animator2.setStartDelay(d);
//            animator2.start();
//        }
//    }


    private static Interpolator gusterpolator;


    private static Interpolator getMaterialInterpolator(Context ctx) {
        if (gusterpolator == null) {
            synchronized (AnimatorUtils.class) {
                if (gusterpolator == null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        gusterpolator = AnimationUtils.loadInterpolator(ctx, android.R.interpolator.fast_out_slow_in);/*################################## SDK level 21 ##########*/
                    } else {
                        gusterpolator = new DecelerateInterpolator();
                    }
                }
            }
        }
        return gusterpolator;
    }
}
